#include "qhistorydatabasewidget.h"

#include "../../libs/core/styledbar.h"
#include "../../libs/core/qactiontoolbar.h"
#include "../../libs/core/qsoftcore.h"
#include "../../libs/device/qdevicemanager.h"
#include "../../libs/core/history_database/qhistorydatabaseview.h"

#include <QVBoxLayout>
#include <QAction>
#include <QMessageBox>
#include <QApplication>
#include <QFile>
#include <QFileDialog>

QHistoryDatabaseWidget::QHistoryDatabaseWidget(tagDeviceInfo *info, QWidget *parent) :
    QWidget(parent),
    m_device_info(*info),
    m_history_database_view(new QHistoryDatabaseView)
{
    QVBoxLayout *v=new QVBoxLayout;
    v->setMargin(0);
    v->setSpacing(0);

    QActionToolBar *tool=new QActionToolBar;

    QVBoxLayout *vv=new QVBoxLayout;
    vv->setMargin(0);
    vv->setSpacing(0);

    QList<QAction*> list;

    QAction* ac=new QAction(this);
    ac->setIcon(QIcon(":/images/device_database_download.png"));
    ac->setText(tr("Download"));
    connect(ac,SIGNAL(triggered()),this,SLOT(load()));
    list.append(ac);

    ac=new QAction(this);
    ac->setIcon(QIcon(":/images/save.png"));
    ac->setText(tr("Save"));
    connect(ac,SIGNAL(triggered()),this,SLOT(save()));
    connect(m_history_database_view,SIGNAL(has_data(bool)),ac,SLOT(setEnabled(bool)));
    ac->setEnabled(false);
    list.append(ac);

    ac=new QAction(this);
    ac->setIcon(QIcon(":/images/clear.png"));
    ac->setText(tr("Clear"));
    connect(ac,SIGNAL(triggered()),m_history_database_view,SLOT(clear()));
    list.append(ac);

    ac=new QAction(this);
    ac->setSeparator(true);
    list.append(ac);

    tool->addButtonActions(list);

    m_history_database_view->set_toolbar(tool);

    v->addWidget(m_history_database_view);
    setLayout(v);
    m_device_manager=QSoftCore::get_core()->get_device_manager();
}

void QHistoryDatabaseWidget::load()
{
    connect(m_device_manager,SIGNAL(database_count(int)),this,SLOT(database_count(int)));
    connect(m_device_manager,SIGNAL(database_end()),this,SLOT(database_end()));
    connect(m_device_manager,SIGNAL(database_error(QString)),this,SLOT(database_error(QString)));
    connect(m_device_manager,SIGNAL(database_next()),this,SLOT(database_next()));
    m_progress_dialog=new QProgressDialog(this,Qt::CustomizeWindowHint | Qt::WindowTitleHint);
    m_progress_dialog->setWindowTitle(tr("Download"));
    m_progress_dialog->setValue(0);
    m_progress_dialog->setCancelButton(NULL);
    m_device_manager->start_load_database(m_device_info.m_uuid,"history_data.db");
    m_progress_dialog->exec();
    delete m_progress_dialog;
}

void QHistoryDatabaseWidget::database_count(int count)
{
    m_progress_dialog->setRange(0,count);
    disconnect(m_device_manager,SIGNAL(database_count(int)),this,SLOT(database_count(int)));
    disconnect(m_device_manager,SIGNAL(database_end()),this,SLOT(database_end()));
    disconnect(m_device_manager,SIGNAL(database_error(QString)),this,SLOT(database_error(QString)));
    disconnect(m_device_manager,SIGNAL(database_next()),this,SLOT(database_next()));
}

void QHistoryDatabaseWidget::database_end()
{
    m_history_database_view->load(qApp->applicationDirPath()+"/temp/"+m_device_info.m_uuid+"/history_data.db");
}

void QHistoryDatabaseWidget::database_error(const QString &error)
{
    m_progress_dialog->close();
    disconnect(m_device_manager,SIGNAL(database_count(int)),this,SLOT(database_count(int)));
    disconnect(m_device_manager,SIGNAL(database_end()),this,SLOT(database_end()));
    disconnect(m_device_manager,SIGNAL(database_error(QString)),this,SLOT(database_error(QString)));
    disconnect(m_device_manager,SIGNAL(database_next()),this,SLOT(database_next()));

    QMessageBox::critical(this,tr("Error"),error);
}

void QHistoryDatabaseWidget::database_next()
{
    m_progress_dialog->setValue(m_progress_dialog->value()+1);
}

void QHistoryDatabaseWidget::save()
{
    QString str=QFileDialog::getSaveFileName(this,tr("Save"),QDir::currentPath(),tr("DB File(*.db)"));
    if(str!="")
    {
        QFile f(str);

        if(!f.open(QFile::ReadWrite))
        {
            QMessageBox::critical(this,tr("Warning"),tr("Save Failed!"));
            return;
        }
        else
        {
            f.resize(0);
            QFile ff(qApp->applicationDirPath()+"/temp/"+m_device_info.m_uuid+"/history_data.db");
            if(!ff.open(QFile::ReadOnly))
            {
                QMessageBox::critical(this,tr("Warning"),tr("Save Failed!"));
                return;
            }
            f.write(ff.readAll());
            f.close();
            ff.close();
        }
    }
}
